<template>
  <div>
    <city-header></city-header>
    <city-search></city-search>
    <city-list :cities = "cities" :hotCities="hotCities"></city-list>
    <city-alphabet :cities = "cities"></city-alphabet>
  </div>
</template>

<script>
import CityHeader from './components/Header'
import CitySearch from './components/Search'
import CityList from './components/List'
import CityAlphabet from './components/Alphabet'
import axios from 'axios'
export default {
  name: 'City',
  data () {
    return {
      cities: {},
      hotCities: []
    }
  },
  components: {
    CityHeader,
    CitySearch,
    CityList,
    CityAlphabet
  },
  mounted () { /* 生命周期函数，ajax代码就写在这里 */
    this.getCityInfo() /* 页面挂载好，首先执行这个函数。这个函数要定义在methods中 */
  },
  computed: {},

  methods: { /* 这里定义函数 */
    getCityInfo () { /* 定义函数的方法 */
      axios.get('/api/city.json') /* 这个get方法帮助我们获取ajax数据，去请求一个url */
        .then(this.handleGetCityInfoSucc) /* 请求完成，axios返回一个对象 */
    },
    handleGetCityInfoSucc (res) {
      res = res.data
      if (res.ret && res.data) {
        const data = res.data
        this.cities = data.cities
        this.hotCities = data.hotCities
      }
      console.log(res) /* 查看这个对象返回的结果 */
    }
  }
}

</script>
<style lang='stylus' scoped>
</style>
